package com.ayoka.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ayoka.R;
import com.jcp.baselibrary.utils.UIUtils;

/**
 * Created by jinchunping on 2018/4/25.
 */
@SuppressLint("NewApi")
public class CircleProgressView extends View {

    private int size;
    private int radius;
    private int circleColor;
    private float circleWidth;
    private int progressColor;
    private int pointColor;
    private float pointSize;

    private Paint circlePaint;
    private Paint progressPaint;
    private Paint pointPaint;
    /*渐变圆周颜色数组*/
    private int[] gradientColorArray = new int[]{
            Color.WHITE,
            Color.LTGRAY,
            Color.WHITE,
            Color.WHITE
    };

    private long maxProgress;
    private long currentProgress;
    private float sweepAngle;

    public CircleProgressView(Context context) {
        super(context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        /**
         * 获得属性
         */
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
        if (typedArray != null) {
            radius = typedArray.getDimensionPixelSize(R.styleable.ProgressView_pv_radius, 100);
            circleColor = typedArray.getColor(R.styleable.ProgressView_pv_circleColor, Color.LTGRAY);
            circleWidth = typedArray.getDimension(R.styleable.ProgressView_pv_circleWidth, UIUtils.getDimensionPixelSize(R.dimen.dp_6));
            progressColor = typedArray.getColor(R.styleable.ProgressView_pv_progressColor, Color.WHITE);
            pointColor = typedArray.getColor(R.styleable.ProgressView_pv_circleWidth, Color.WHITE);
            pointSize = typedArray.getDimension(R.styleable.ProgressView_pv_pointSize, (int) UIUtils.getDimensionPixelSize(R.dimen.dp_20));
        }
        typedArray.recycle();
        initPaint();
    }

    private void initPaint() {
        circlePaint = new Paint();
        circlePaint.setColor(circleColor);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setAntiAlias(true);
        circlePaint.setStrokeWidth(circleWidth);

        progressPaint = new Paint();
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeWidth(circleWidth);

        pointPaint = new Paint();
        pointPaint.setAntiAlias(true);
        pointPaint.setColor(pointColor);
        pointPaint.setStyle(Paint.Style.FILL);
    }

    @SuppressWarnings("unused")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        size = Math.min(width, height);
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(size / 2, size / 2);
        drawCircle(canvas);
        drawProgress(canvas);
        drawPoint(canvas);
    }

    private void drawPoint(Canvas canvas) {
        // 通过三角函数计算终点坐标
        float translateX = (float) (Math.sin(Math.PI * sweepAngle / 180) * radius);
        float translateY = (float) (Math.cos(Math.PI * sweepAngle / 180) * radius);
        // 设置阴影
        pointPaint.setShadowLayer(20, 0, 0, Color.LTGRAY);
        canvas.drawCircle(translateX, -translateY, pointSize / 2, pointPaint);
    }

    private void drawCircle(Canvas canvas) {
        // 绘制圆环
        canvas.drawCircle(0, 0, radius, circlePaint);
    }

    private void drawProgress(Canvas canvas) {
        // 绘制圆环
        RectF oval = new RectF(-radius, -radius, radius, radius);
        canvas.drawArc(oval, -90, sweepAngle, false, progressPaint); //
    }

    public void setMaxProgress(long maxProgress) {
        this.maxProgress = maxProgress;
    }

    public void setProgress(long progress) {
        if (maxProgress == 0) return;
        this.currentProgress = progress > maxProgress ? maxProgress : progress;
        sweepAngle = currentProgress * 360 / maxProgress;
        postInvalidate();
    }

    public long getCurrentProgress() {
        return currentProgress;
    }
}
